<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/3.3.7/css/bootstrap.min.css">
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script type="text/javascript" src="/webjars/axios/0.19.0/dist/axios.js"></script>
    <style>

    </style>
</head>
<body>


<div id="vuebox">
    <div class="container">
        <div class="row" style="margin-top: 50px;">
            <div class="col-md-6">
                <form class="form-inline" role="form" style="padding-left: 250px;">
                    <div class="form-group" style="padding-left: 20px;">
                        <label>新闻分类:</label>
                        <select class="form-control" v-model="categoryId"><!--骆驼命名法-->
                            <option value="0">全部</option>
                            <option v-for="item in categoryList.data" :value="item.id">{{item.name}}</option>
                        </select> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
                        新闻标题:<input class="form-control" type="text" name="title" v-model="title">
                    </div>
                    <div class="form-group" style="padding-left: 20px;">
                        <button type="button" class="btn btn-primary" @click="selectCategory(1)">查询</button>
                    </div>

                </form>
            </div>
            <div class="col-md-4 text-right">
                <a @click.stop="showModal()" class="btn btn-primary">新增电子图书</a>
            </div>
        </div>

        <br/>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>新闻编号</th>
                <th>新闻名称</th>
                <th>新闻摘要</th>
                <th>作者</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="item in detailList.data.list">
                <td>{{item.id}}</td>
                <td>{{item.title}}</td>
                <td>{{item.summary}}</td>
                <td>{{item.author}}</td>
                <td>{{item.updatedate}}</td>
                <td>
                    <button class="btn btn-success" @click="to_update_entry(item.id)">修改</button>
<!--                    <button class="btn btn-danger" @click="deleteEntry(item.id)">删除</button>-->
                </td>
            </tr>
            </tbody>
        </table>
        <br/>


        <div class="text-center">
            <ul class="pagination">
                <li v-show="detailList.data.hasPreviousPage">
                    <a href="#" @click.stop="selectCategory(detailList.data.perPage)">&laquo;</a>
                </li>
                <li :class="{active:detailList.data.pageNum==i}" v-for="i of detailList.data.navigatepageNums">
                    <a href="#" @click.stop="selectCategory(i)">{{i}}</a>
                </li>
                <li v-show="detailList.data.hasNextPage">
                    <a href="#" @click.stop="selectCategory(detailList.data.nextPage)">&raquo;</a>
                </li>
            </ul>
        </div>
    </div>





    <!--添加模态框-->
    <!--模态框-->
    <div class="modal" id="mymodal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" type="button" @click="hideModal()">&times;</button>
                <div class="modal-title">
                    更新新闻信息
                </div>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">

                    <div class="form-group">
                        <label for="detail_id" class="col-sm-2 control-label">新闻编号</label>
                        <div class="col-sm-10">
                    <input type="text"  class="form-control"  id="detail_id" v-model="new_detail.id"  >

                        </div>
                    </div>


                     <div class="form-group">
                         <label for="categoryId" class="col-sm-2 control-label">新闻分类</label>
                         <div class="col-sm-10">
                             <select id="categoryId" class="form-control" v-model="new_detail.categoryid">
                                 <option value="0">全部</option>
                                 <option v-for="item in categoryList.data" :value="item.id">{{item.name}}</option>
                             </select>
                         </div>
                     </div>
                    <div class="form-group">
                        <label for="title" class="col-sm-2 control-label">新闻标题</label>
                        <div class="col-sm-10">
                            <input v-model="new_detail.title" id="title" type="text" class="form-control" placeholder="请输入图书名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="summary" class="col-sm-2 control-label">新闻摘要</label>
                        <div class="col-sm-10">
                            <input v-model="new_detail.summary" id="summary" type="text" class="form-control" placeholder="请输入图书摘要"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="author" class="col-sm-2 control-label">作者</label>
                        <div class="col-sm-10">
                            <input v-model="new_detail.author" id="author" type="text" class="form-control" placeholder="请输入上传人">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" @click="hideModal()">关闭</button>
                <button class="btn btn-primary" @click="saveEntry()">保存</button>
            </div>
        </div>
    </div>
</div>



</div>

</body>
</html>
<script>
    var vm = new Vue({
        el: "#vuebox",
        data: {
            categoryList: {},
            categoryId: "0",
            title: "",
            detailList:{},
            delResult:{},
            new_detail:{id:"",categoryid:"0",title:"",summary:"",author:""},
        },
        methods: {
            selectCategory:function (pageNum) {
                axios.get("/pageInfo",{params:{categoryId: this.categoryId,title: this.title,pageNum:pageNum}})
                    .then(res=>{
                        this.detailList=res.data;
                         this.pageNum=pageNum;
                    })
            },
            showModal:function () {
                $("#mymodal").modal("show");
            },
            hideModal:function () {
                $("#mymodal").modal("hide");
                //清除模态框内容
                this.clearForm();
            },
            clearForm:function () {
                $("#detail_id").val("");
                $("#categoryId").val(0);
                $("#title").val("");
                $("#summary").val("");
                $("#autor").val("");
            },
            saveEntry:function () {
            axios.post("/update",this.new_detail).then(res=>{
                this.delResult=res.data;
                if(this.delResult.result="SUCCESS"){
                    //增加成功默认到第一页
                    this.selectCategory(this.pageNum);
                    //增加成功关闭模态框
                    this.hideModal();
                }else{
                    alert(this.delResult.message);
                }
            })
            },
            to_update_entry:function (id) {
                axios.get("/selectById",{params:{id:id}}).then(res=>{
                    this.new_detail=res.data.data;
                    this.showModal();
                })
            }
        },
        mounted() {
            axios.get("/selectAll").then(res => {
                this.categoryList = res.data
            });
           this.selectCategory(1);

        }
    })
</script>